<?php
session_start();
mysql_connect("localhost","root","");
mysql_select_db("semtimes");

$lmt=10;
header("Content-Type:text/plain");

$dom=domxml_new_doc('1.0');
$root=$dom->create_element("QuizGame");
  $query="select distinct level_id from questionbank order by level_id";
  $res=mysql_query($query);
  while($row=mysql_fetch_object($res))
  {
  $level=$dom->create_element("Level");  //level tag
  $level->set_attribute("lid","$row->level_id");
  $inrres=mysql_query("select question_id from question_set where question_level= $row->level_id and user_id =$_SESSION[user_id]");
  $qids="";
	while($inrrow=mysql_fetch_object($inrres))
	{
		$qids=$qids.$inrrow->question_id.',';
	}
	$qids=substr($qids,0,strlen($qids)-1);
    $query="select * from questionbank where level_id=$row->level_id and question_id not in($qids) order by RAND() limit $lmt";
          // echo $query;
    $res1=mysql_query($query);
	if(!$res1 || mysql_num_rows($res1)<$lmt)
        {
                $query="select * from questionbank where level_id=$row->level_id order by RAND() limit $lmt";
                $res1=mysql_query($query);
        }
    while($row1=mysql_fetch_array($res1))
    {
    $ques=$dom->create_element("question");  //Question TAg
    $ques->set_attribute("qid","$row1[question_id]");
      $stmt_tag=$dom->create_element("statement");  //statement tag
      $stmt_node=$dom->create_text_node("$row1[question_statement]");
      $stmt_tag->append_child($stmt_node);
      $ques->append_child($stmt_tag);

      $opts_tag=$dom->create_element("options");  //options tag
      FOR($i=1;$i<=4;$i++)
      {
        $opt_tag=$dom->create_element("option");  //option tag
        $opt_tag->set_attribute("value","$i");
        $field="opt".$i;
        $val=$row1[$field];
        $opt_node=$dom->create_text_node("$val");
        $opt_tag->append_child($opt_node);
        $opts_tag->append_child($opt_tag);
      }
      $ques->append_child($opts_tag);

      $exp_tag=$dom->create_element("explanation");  //explanation tag
      $exp_node=$dom->create_text_node("$row1[explanation]");
      $exp_tag->append_child($exp_node);
      $ques->append_child($exp_tag);

      $crct_tag=$dom->create_element("correct");  //correct tag
      $crct_attr=$dom->create_text_node("$row1[ans]");
      $crct_tag->append_child($crct_attr);
      $ques->append_child($crct_tag);

      $atmt_tag=$dom->create_element("attempt");  //attempt tag
      $atmt_attr=$dom->create_text_node("0");
      $atmt_tag->append_child($atmt_attr);
      $ques->append_child($atmt_tag);

      $marks_tag=$dom->create_element("marks");  //marks tag
      $marks_attr=$dom->create_text_node("0");
      $marks_tag->append_child($marks_attr);
      $ques->append_child($marks_tag);
          $level->append_child($ques);
      }
$root->append_child($level);
}
    
$dom->append_child($root);
$dt=time();
$fname="ques_group_".$_SESSION['user_id']."_".$dt.'.xml';
$_SESSION['testtime']=date("Y-m-d",$dt);
$_SESSION['ques_group']=$fname;
$_SESSION['savedres']='';
$dom->dump_file('/var/www/semtimes.com/game/'.$fname,false,false);
//$xml_string = $dom->dump_mem(true);
echo "&XMLName=".$fname;
//echo $xml_string;
//  xml write
//$fp=fopen('/var/www/html/logs/semtimesGame/fname.xml','w');
//fprintf($fp,"%s",$xml_string);
//fclose($fp);
?>
